﻿using System;

namespace ThresholdAssistant.Dapper.Extensions
{
    /// <summary>
    /// SQL字符串扩展
    /// </summary>
    static class StringsExtensions
    {
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="input">输入脚本</param>
        /// <param name="startStr">开始的字符(包含)</param>
        /// <returns></returns>
        public static string SubString(this string input, string startStr)
        {
            var comparisonType = StringComparison.InvariantCultureIgnoreCase;
            var index = input.IndexOf(startStr, comparisonType);
            return input.Substring(index);
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="input">输入脚本</param>
        /// <param name="startStr">开始的字符(包含)</param>
        /// <param name="endStr">结束的字符(不包含)</param>
        /// <returns></returns>
        public static string SubString(this string input, string startStr, string endStr)
        {
            var comparisonType = StringComparison.InvariantCultureIgnoreCase;
            var startIndex = input.IndexOf(startStr, comparisonType);
            var endIndex = input.IndexOf(endStr, comparisonType);
            return input.Substring(startIndex, endIndex - startIndex);
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="input">输入脚本</param>
        /// <param name="rightStrList">右边的字符(不包含)</param>
        /// <returns></returns>
        public static string RemoveRight(this string input, params string[] rightStrList)
        {
            foreach (var rightStr in rightStrList)
            {
                var comparisonType = StringComparison.InvariantCultureIgnoreCase;
                var lastIndex = input.LastIndexOf(rightStr, comparisonType);
                if (lastIndex <= 0)
                    continue;

                input = input.Substring(0, lastIndex);
                if (string.IsNullOrEmpty(input))
                {
                    break;
                }
            }
            return input;
        }
    }
}